package org.example.esay;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

import java.util.HashMap;
import java.util.Objects;

/**
 *  大数字计算，
 *    简单算法题
 *      2022873723823  + 28837192093  不能用大数承接,如何计算出结果, 超大的数字;
 * Create by haocs on 2025/4/9.
 */
public class ServletInitializer  {

    public static void main(String[] args) {

        String a = "1176234";
        String b = "6660021126";
        String[] splita = a.split("");
        String[] splitb = b.split("");
        /* 按 个十百千万 切割 做数组 */
        HashMap<Integer, Integer> numa = getNum(splita);
        HashMap<Integer, Integer> numb = getNum(splitb);

        if (numa.isEmpty() && numb.isEmpty()) {
            System.out.println("erro");
        }
        /*  如果A 为空  输出B  */
        if (numa.isEmpty()) {
            System.out.println(b);
        }
        if (numb.isEmpty()) {
            System.out.println(a);
        }
        /**
         * 做计算
         */
        int lenth = numa.size() > numb.size() ? numb.size() : numa.size();

        HashMap<Integer, Integer> resultMap = new HashMap<>();
        for (int i = 0; i < lenth; i++) {

            Integer ai = numa.get(i);
            Integer bi = numb.get(i);
            int ci = ai + bi;
            if (!Objects.isNull(resultMap.get(i))) {
                Integer ki = resultMap.get(i);
                ci += ki;
            }
            if (ci < 10) {
                resultMap.put(i, ci);
            } else if (ci >= 10) {
                int di = ci / 10;
                int ei = ci % 10;
                resultMap.put(i, ei);
                int tempi = i;
                resultMap.put(++tempi, di);
            }
        }
        StringBuilder reserver = reserver(resultMap);
        StringBuilder sub = new StringBuilder();
        String s = setResult(numa, numb, lenth);
        sub.append(s).append(reserver);
        System.out.println(sub);
    }

    /**
     * 翻转计算结果
     *
     * @param resultMap
     * @return
     */
    private static StringBuilder reserver(HashMap<Integer, Integer> resultMap) {
        StringBuilder result = new StringBuilder();
        int size = resultMap.size();
        for (int i = size - 1; i >= 0; i--) {
            Integer integer = resultMap.get(i);
            String s = String.valueOf(integer);
            if (StringUtils.isNotBlank(s)) {
                result.append(s);
            }
        }
        return result;
    }

    /**
     * 处理 两数 位数不相等 计算处理
     *
     * @param numa
     * @param numb
     * @param lenth
     * @return
     */
    private static String setResult(HashMap<Integer, Integer> numa, HashMap<Integer, Integer> numb, int lenth) {
        String sub = "";
        if (numa.size() > lenth) {
            // 1 2 3 4 5 6 7 8 9 0 -1
            sub = setNums(numa, lenth);

        } else if (numb.size() > lenth) {
            sub = setNums(numb, lenth);

        }
        return sub;
    }

    /**
     * 赋值结果
     *
     * @param numa
     * @param length
     */
    private static String setNums(HashMap<Integer, Integer> numa, int length) {
        StringBuilder tempSub = new StringBuilder();
        for (int i = numa.size() - 1; i >= length; i--) {
            Integer integer = numa.get(i);
            String s = String.valueOf(integer);
            if (StringUtils.isNotBlank(s)) {
                tempSub.append(s);
            }
        }
        return tempSub.toString();
    }

    /**
     * 按 个十百千万 切割 做数组
     *
     * @param splita
     * @return
     */
    private static HashMap<Integer, Integer> getNum(String[] splita) {
        /*  倒叙 装入 map , key=位 value = 值*/
        HashMap<Integer, Integer> map = new HashMap<>();
        int index = 0;
        for (int i = splita.length - 1; i >= 0; i--) {
            int tempa = Integer.valueOf(splita[i]);
            map.put(index, tempa);
            index++;
        }
        return map;
    }
}
